﻿using System;
using UnityEngine;
using UnityEngine.UI;

namespace  Panda.UI.Runtime.ElementExpands
{

    public class UITabViewScript : MonoBehaviour
    {
        [SerializeField]
        private Toggle[] categoryToggles = Array.Empty<Toggle>();   
        [SerializeField]
        private UITabPanelItemBaseScript[] panelItems = Array.Empty<UITabPanelItemBaseScript>();

        private int _lastSelectedIndex = -1;
        
        public void Setup(int firstPanelIndex)
        {
            for (int i = 0; i < panelItems.Length; i++)
            {
                panelItems[i].OnSetup(this);
            }

            SelectCategory(firstPanelIndex, true);

            for (int i = 0; i < categoryToggles.Length; i++)
            {
                categoryToggles[i].onValueChanged.AddListener(UI_OnToggleChanged);
            }
        }

        public UITabPanelItemBaseScript GetCurrentTabPanelItem()
        {
            if (_lastSelectedIndex < 0 || _lastSelectedIndex >= panelItems.Length)
            {
                return null;
            }
            
            return panelItems[_lastSelectedIndex];
        }

        public void Cleanup()
        {
            for (int i = 0; i < categoryToggles.Length; i++)
            {
                categoryToggles[i].onValueChanged.RemoveListener(UI_OnToggleChanged);
            }
        }

        public void UI_OnNavPrev()
        {
            if (_lastSelectedIndex <= 0)
            {
                return;
            }
            
            SelectCategory(_lastSelectedIndex - 1, false);
        }

        public void UI_OnNavNext()
        {
            if (_lastSelectedIndex >= categoryToggles.Length - 1)
            {
                return;
            }
            
            SelectCategory(_lastSelectedIndex + 1, true);
        }

        private void UI_OnToggleChanged(bool _)
        {
            for (int i = 0; i < categoryToggles.Length; i++)
            {
                if (i == _lastSelectedIndex)
                {
                    continue;
                }

                var toggle = categoryToggles[i];
                if (toggle.isOn == false)
                {
                    continue;
                }
                
                SelectCategory(i, false);
                break;
            }
        }
        
        private void SelectCategory(int index, bool forceRefresh)
        {
            if (index < 0 || index >= categoryToggles.Length)
            {
                Debug.LogError($"SettingsWindowController: Invalid index {index}, fallback 0!");
                index = 0;
            }
            
            if (_lastSelectedIndex == index && !forceRefresh)
            {
                return;
            }

            for (int i = 0; i < categoryToggles.Length; i++)
            {
                var shouldActive = i == index;

                if (categoryToggles[i].isOn != shouldActive)
                {
                    categoryToggles[i].SetIsOnWithoutNotify(shouldActive);
                }

                var panelItem = panelItems[i];
                if (shouldActive == false && panelItem.gameObject.activeSelf)
                {
                    panelItem.OnHidePanel();
                    panelItem.gameObject.SetActive(false);
                }
                if (shouldActive && (panelItem.gameObject.activeSelf == false || forceRefresh))
                {
                    panelItem.gameObject.SetActive(true);
                    panelItem.OnShowPanel();
                }
            }

            _lastSelectedIndex = index;
        }
        
    }
    
}